import csv
import unittest
import tsv

class TestTsvReading(unittest.TestCase):

    def setUp(self):
        self.testfile=open("test.txt", "r")
        self.correct_schema = ["A", "B", "C", "D", "E", "F"]

    def testMatched(self):
        reader = tsv.TsvReader(self.testfile, self.correct_schema)
        rows = 0
        for row in reader:
            rows += 1
            for x in range(0,6):
                self.assertFalse(int(row[x]) % (x+1), "stupid value check")
                self.assertEqual(row[x], row[self.correct_schema[x]], "schema identity check")
        self.assertEqual(rows, 4, "number of rows! " + str(rows))

    def testReversed(self):
        reader = tsv.TsvReader(self.testfile, ["F", "E", "D", "C", "B", "A"])
        self.assertEqual(reader.schemaMap, [5,4,3,2,1,0])
        rows = 0
        for row in reader:
            rows += 1
            for x in range(0, 6):
                self.assertFalse(int(row[x]) % (6-x), "stupid value check")
                self.assertEqual(row[x], row[self.correct_schema[5-x]], "schema reverse identity check")
        self.assertEqual(rows, 4, "number of rows! " + str(rows))

    def testScrambled(self):
        reader = tsv.TsvReader(self.testfile, ["B", "E", "E", "F", "C", "A", "K", "E"])
        self.assertEqual(reader.schemaMap, [1,4,4,5,2,0,None,4])
        self.assertEqual([x for x in reader.next()], ['2','5','5','6','3','1',None,'5'])

    def testStraightUpNameIndexing(self):
        reader = tsv.TsvReader(self.testfile, ["Bogus"])
        self.assertEqual(reader.schemaMap, [None])
        rows = 0
        for row in reader:
            rows += 1
            for x in range(0, 6):
                self.assertFalse(int(row[self.correct_schema[x]]) % (x+1), "stupid value check")
        self.assertEqual(rows, 4)

    def testSynonyms(self):
        reader = tsv.TsvReader(self.testfile,
            [
                ["A", "alpha"],
                ["B", "beta"],
                ["D", "delta"],
                ["Q", "quadriceps"],
                ["F", "fabulous"]
            ]
        )
        self.assertEqual(reader.schemaMap, [0,1,3,None,5])
        rows = 0
        for row in reader:
            rows += 1
            self.assertEqual(len(row), 5)
            self.assertEqual(row[3], None)
            self.assertFalse(int(row[2]) % 2)
            self.assertEqual(row["D"], row["delta"])
            self.assertEqual(row["fabulous"], row[4])
        self.assertEqual(rows, 4)

    def testFixRaggedTable(self):
        raggedIn = open("ragged.txt", "rb")
        fixedOut = open("raggedOut.txt", "wb")
        tsv.fixRaggedTable(raggedIn, fixedOut, csv.excel)
        fixedOut.flush()
        fixedOut.close()
        raggedIn.close()
        del raggedIn
        fixedIn = open("raggedOut.txt", "rb")
        fixedReader = csv.reader(fixedIn)
        for row in fixedReader:
            self.assertEqual(6, len(row), str(row) + " didn't pad")

if __name__ == '__main__':
    unittest.main()